part of _;

extension on BindPage {
  void controller_initData() {
    yPost<BindStatusModel>(IFinal.Jkids.getBindStatus, null, (isSuccess, results, code, message) {
      if (isSuccess)
        setState(() => model = results!.first);
      else
        yShowBuilder("数据加载失败，是否重试？\n$message", () => controller_initData(), () => yFinish(), false);
    }, onModel: (map) => BindStatusModel.fromJson(map), showProgress: false, cacheEnable: true);
  }

  void _bindSuccess(UserModel userModel) {
    yToast("绑定成功");
    yDismissDialog_unDismissible();
    AppUtils.saveUserModel(userModel);
    controller_initData();
  }

  void controller_showBindPhone() {
    yShowSelectBuilder("请选择验证方式", true, [Enum_LoginType.Verify.rawValue!, Enum_LoginType.Code_server.rawValue!, Enum_LoginType.Code_sdk.rawValue!], (index, action) {
      switch (Enum_LoginType.from(action)) {
        case Enum_LoginType.Verify:
          AppUtils.check_verify(yPage, (userModel) => _bindSuccess(userModel));
          break;
        case Enum_LoginType.Code_server:
        case Enum_LoginType.Code_sdk:
          Timer? timer_code;
          var codeTimeLeft = 0;
          final controller_phone = TextEditingController(), controller_code = TextEditingController();
          yShowStatefulDialog(
            dismissible: false,
            alertBuilder: (pageContext, dialogContext, statefulContext, setDialogState) {
              void runSmsTimer() {
                setDialogState(() => codeTimeLeft = 60);
                timer_code = yRunLoop((timer) {
                  if (codeTimeLeft > 0)
                    setDialogState(() => codeTimeLeft--);
                  else
                    timer_code?.cancel();
                }, 1000);
              }

              return AlertDialog(
                content: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    TextFormField(
                      controller: controller_phone,
                      keyboardType: TextInputType.phone,
                      decoration: InputDecoration(hintText: "请输入手机号"),
                    ),
                    TextFormField(
                      controller: controller_code,
                      keyboardType: TextInputType.phone,
                      decoration: InputDecoration(
                        hintText: "请输入验证码",
                        suffixIcon: YButton(
                          child: Text(codeTimeLeft > 0 ? "${codeTimeLeft}s后获取" : "获取$action", style: TextStyle(color: YConfig.themeColor)),
                          enable: codeTimeLeft <= 0,
                          onTap: () {
                            switch (action) {
                              case "Server验证码":
                                AppUtils.send_sms_server(yPage, controller_phone.text, () => runSmsTimer());
                                break;
                              case "SDK验证码":
                                AppUtils.send_sms_sdk(controller_phone.text, () => runSmsTimer());
                                break;
                            }
                          },
                        ),
                      ),
                    ),
                  ],
                ),
                actions: [
                  YButton(
                    child: Text("取消"),
                    onTap: () {
                      timer_code?.cancel();
                      yDismissDialog_unDismissible();
                    },
                  ),
                  YButton(
                    child: Text("绑定"),
                    onTap: () {
                      switch (action) {
                        case "Server验证码":
                          AppUtils.check_sms_server(yPage, controller_phone.text, controller_code.text, (userModel) {
                            timer_code?.cancel();
                            _bindSuccess(userModel);
                          });
                          break;
                        case "SDK验证码":
                          AppUtils.check_sms_sdk(yPage, controller_phone.text, controller_code.text, (userModel) {
                            timer_code?.cancel();
                            _bindSuccess(userModel);
                          });
                          break;
                      }
                    },
                  ),
                ],
              );
            },
          );
          break;
        default:
          yToast("尚未支持的登录方式");
          break;
      }
    });
  }

  void controller_showBindThird(ShareSDKPlatform thirdPlatform) {
    AppUtils.check_third(yPage, thirdPlatform, (userModel) => _bindSuccess(userModel));
  }
}
